<template>
  <div class="container">
    <div class="btnBox">
      <a-button htmlType="submit" type="primary" @click="addAndEdit('add')">+添加会员等级</a-button>
    </div>
    <div class="team_management">
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        bordered
        :rowKey="(record) => record.id"
      >
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="primary" @click="addAndEdit('edit', record)">编辑</a-button>
        </template>
      </s-table>
      <a-modal :title="title" v-model="visible" @ok="handleOk" :maskClosable="false">
        <a-form :form="form" class="permission-form">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="会员等级">
            <a-input
              v-decorator="[
              'memberLevel',
              {
                rules: [
                  {
                    required: true,
                  },
                ],
                initialValue: title === '添加会员等级' ? vipLevel : info.memberLevel,
              },
            ]"
              disabled
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总充值金额">
            <a-input
              v-decorator="[
              'rechargeAmount',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入总充值金额',
                  },
                ],
                initialValue: title === '添加会员等级' ? '' : info.rechargeAmount,
              },
            ]"
              placeholder="请输入总充值金额"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="每日签到">
            <a-input
              v-decorator="[
              'checkinAmount',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入每日签到',
                  },
                ],
                initialValue: title === '添加会员等级' ? '' : info.checkinAmount,
              },
            ]"
              placeholder="请输入每日签到"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="项目额外收益（%）">
            <a-input
              v-decorator="[
              'additionalAmount',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入项目额外收益',
                  },
                ],
                initialValue: title === '添加会员等级' ? '' : info.additionalAmount,
              },
            ]"
              placeholder="请输入项目额外收益"
            />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
  </div>
</template>

<script>
import STable from '@/components/Table'
import { VipMemberList, VipMemberListDetail, VipMemberListAdd, VipMemberListEdit } from '@/api/configuration-management'
import { mapGetters } from 'vuex'
// import { common } from '@/common/index'

export default {
  name: 'teamManagement',
  components: {
    STable
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    return {
      form: this.$form.createForm(this),
      formLayout: 'horizontal',
      labelCol: {
        xs: { span: 8 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      vipLevel: 0,
      title: '',
      info: {},
      columns: [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '会员等级',
          dataIndex: 'memberLevel',
          align: 'center'
        },
        {
          title: '总充值金额',
          dataIndex: 'rechargeAmount',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '每日签到',
          dataIndex: 'checkinAmount',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '项目额外收益（%）',
          dataIndex: 'additionalAmount',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          slots: { title: 'action' },
          scopedSlots: { customRender: 'action' },
          align: 'center'
        }
      ],
      // 查询条件参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const { userId } = this.userInfo
        let params = {
          ...parameter,
          userId
        }
        return new Promise((resolve, reject) => {
          VipMemberList(params)
            .then((res) => {
              if (res.code === 200) {
                this.vipLevel = res.data.total
                resolve(res.data)
              } else {
                this.$message.error(res?.msg)
              }
            })
            .catch((err) => {
              reject(err)
            })
            .finally(() => {
            })
        })
      }
    }
  },
  methods: {
    addAndEdit(type, record = {}) {
      this.form.resetFields()
      if (type === 'edit') {
        const { userId } = this.userInfo
        const params = {
          memberId: record.memberId,
          userId
        }
        VipMemberListDetail(params).then((res) => {
          if (res.code === 200) {
            this.info = res.data ?? {}
          }
        })
      } else {
        this.info = {}
      }
      this.title = type === 'edit' ? '修改会员等级' : '添加会员等级'
      this.visible = true
    },
    refresh(isBackToFirstPage = false) {
      this.$refs.table.refresh(isBackToFirstPage)
    },
    search() {
      this.refresh(false)
    },
    searchReset() {
      this.queryParam = {}
      this.refresh(true)
    },

    handleOk(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          const { userId } = this.userInfo
          let params = {
            memberId: this.info.memberId ?? '',
            additionalAmount: +values.additionalAmount,
            checkinAmount: +values.checkinAmount,
            memberLevel: +values.memberLevel,
            rechargeAmount: +values.rechargeAmount,
            userId
          }
          if (this.title === '添加会员等级') {
            delete params.memberId
          }
          let url = this.title === '添加会员等级' ? VipMemberListAdd : VipMemberListEdit
          url(params).then((res) => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.visible = false
              this.info = {}
              this.$refs.table.refresh()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.team_management {
  .delete {
    color: #e94141;
    margin-left: 15px;
  }

  .form {
    display: flex;
    margin-bottom: 20px;
    flex-wrap: wrap;
  }

  .red {
    color: #e94141;
  }

  .green {
    color: #46c16a;
  }

  .permission-form {
    :deep(.permission-group) {
      margin-top: 0;
      margin-bottom: 0;
    }
  }
}

.modal {
  /deep/ .ant-modal {
    width: 1000px !important;
  }

  .user_harder_img {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    span {
      padding-left: 15px;
    }

    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
    }
  }

  .user_info {
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;
    border-bottom: 1px solid #f3f4f6;

    &:last-child {
      border-bottom: none;
    }

    .item {
      display: flex;

      div {
        height: 40px;
        width: 50%;
      }

      span {
        &:first-child {
          width: 100px;
          text-align: left;
          padding: 0 15px;
        }

        &:last-child {
          text-align: left;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }

      .textarea {
        width: 80%;
      }
    }
  }
}
</style>
